'use client'

import { Fragment, useState } from 'react'
import { Dialog, Transition, Switch } from '@headlessui/react'
import { useForm } from 'react-hook-form'
import { FormInput } from '@/components/ui/form'
import { updateSupplier } from '@/actions/supplier'
import { Supplier } from '@prisma/client'

interface EditSupplierDialogProps {
  supplier: Supplier
  open: boolean
  onClose: () => void
}

interface SupplierFormData {
  name: string
  contact: string
  phone: string
  address: string
  status: boolean
}

export function EditSupplierDialog({ supplier, open, onClose }: EditSupplierDialogProps) {
  const [submitting, setSubmitting] = useState(false)
  const { register, handleSubmit, formState: { errors } } = useForm<SupplierFormData>({
    defaultValues: {
      name: supplier.name,
      contact: supplier.contact,
      phone: supplier.phone,
      address: supplier.address,
      status: supplier.status
    }
  })

  const onSubmit = async (data: SupplierFormData) => {
    try {
      setSubmitting(true)
      const result = await updateSupplier(supplier.id, data)
      if (result.success) {
        onClose()
      } else {
        alert(result.error)
      }
    } catch (error) {
      console.error('更新供应商失败:', error)
      alert('更新供应商失败')
    } finally {
      setSubmitting(false)
    }
  }

  return (
    <Transition show={open} as={Fragment}>
      <Dialog onClose={onClose} className="relative z-50">
        <Transition.Child
          as={Fragment}
          enter="ease-out duration-300"
          enterFrom="opacity-0"
          enterTo="opacity-100"
          leave="ease-in duration-200"
          leaveFrom="opacity-100"
          leaveTo="opacity-0"
        >
          <div className="fixed inset-0 bg-black/30" />
        </Transition.Child>

        <div className="fixed inset-0 overflow-y-auto">
          <div className="flex min-h-full items-center justify-center p-4">
            <Transition.Child
              as={Fragment}
              enter="ease-out duration-300"
              enterFrom="opacity-0 scale-95"
              enterTo="opacity-100 scale-100"
              leave="ease-in duration-200"
              leaveFrom="opacity-100 scale-100"
              leaveTo="opacity-0 scale-95"
            >
              <Dialog.Panel className="w-full max-w-md transform overflow-hidden rounded-lg bg-white shadow-xl transition-all">
                <div className="border-b border-gray-200 bg-white px-6 py-4">
                  <Dialog.Title as="h3" className="text-lg font-medium text-gray-900">
                    编辑供应商
                  </Dialog.Title>
                </div>

                <form onSubmit={handleSubmit(onSubmit)}>
                  <div className="bg-white px-6 py-6 space-y-6">
                    <FormInput
                      label="供应商名称"
                      error={errors.name?.message}
                      {...register('name', { required: '请输入供应商名称' })}
                    />
                    <FormInput
                      label="联系人"
                      error={errors.contact?.message}
                      {...register('contact', { required: '请输入联系人' })}
                    />
                    <FormInput
                      label="联系电话"
                      error={errors.phone?.message}
                      {...register('phone', { required: '请输入联系电话' })}
                    />
                    <FormInput
                      label="地址"
                      error={errors.address?.message}
                      {...register('address', { required: '请输入地址' })}
                    />
                    <Switch.Group>
                      <div className="flex items-center justify-between">
                        <Switch.Label className="text-sm font-medium text-gray-900">状态</Switch.Label>
                        <Switch
                          {...register('status')}
                          className={`${
                            supplier.status ? 'bg-blue-600' : 'bg-gray-200'
                          } relative inline-flex h-6 w-11 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2`}
                        >
                          <span
                            className={`${
                              supplier.status ? 'translate-x-6' : 'translate-x-1'
                            } inline-block h-4 w-4 transform rounded-full bg-white transition-transform`}
                          />
                        </Switch>
                      </div>
                    </Switch.Group>
                  </div>

                  <div className="border-t border-gray-200 bg-gray-50 px-6 py-4 flex justify-end gap-4">
                    <button
                      type="button"
                      onClick={onClose}
                      className="inline-flex justify-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2"
                    >
                      取消
                    </button>
                    <button
                      type="submit"
                      disabled={submitting}
                      className="inline-flex justify-center rounded-md border border-transparent bg-blue-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:opacity-50"
                    >
                      {submitting ? '提交中...' : '保存'}
                    </button>
                  </div>
                </form>
              </Dialog.Panel>
            </Transition.Child>
          </div>
        </div>
      </Dialog>
    </Transition>
  )
} 